package com.ecode.ocr.service.impl;

import com.ecode.ocr.config.JdCloudConfig;
import com.ecode.ocr.utils.FileUtils;
import com.google.api.client.http.HttpResponse;
import com.google.gson.Gson;
import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.ecode.ocr.domain.business.BusinessResultDTO;
import com.ecode.ocr.domain.common.JdCloudResultDTO;
import com.ecode.ocr.domain.idCard.IdCardResultDTO;
import com.ecode.ocr.domain.invoice.InvoiceResultDTO;
import com.ecode.ocr.service.JdCloudService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wqh
 */
@Service
public class JdCloudServiceImpl implements JdCloudService {

    @Resource
    JdCloudConfig jdCloudConfig;
//
//    @Resource
//    RemoteFileService remoteFileService;

    /**
     * 获取身份证信息
     *
     * @param multipartFile
     * @author: wqh
     * @param: multipartFile
     * @return: List<IdCardResultDTO>
     */
    @Override
    public List<IdCardResultDTO> getIdCardInfo(MultipartFile multipartFile) throws Exception {
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();

        File file = FileUtils.multipartFileToFile(multipartFile);
//        R<SysFile> sysFileR = remoteFileService.upload(multipartFile);
//        System.out.println("=======================");
//        System.out.println(sysFileR.getData());
//        System.out.println("=======================");
        try {
            HttpResponse response = JdcloudSDKClient.upload(jdCloudConfig.accessKey, jdCloudConfig.secretKey, Protocol.HTTPS, jdCloudConfig.endPoint,
                    jdCloudConfig.idCardPath, jdCloudConfig.method, headers, queryMap, file);
            String result = new String(BinaryUtils.toByteArray(response.getContent()));
            System.out.println(result);
            JdCloudResultDTO<List<IdCardResultDTO>> jdCloudResultDTO = new Gson().fromJson(result, JdCloudResultDTO.class);
            System.out.println(jdCloudResultDTO.getResult().getResultData());
            FileUtils.delTempFile(file);
            return jdCloudResultDTO.getResult().getResultData();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 营业执照识别
     *
     * @param multipartFile
     * @author: wqh
     * @param: multipartFile
     * @return:
     */
    @Override
    public Object getBusinessInfo(MultipartFile multipartFile) throws Exception {
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();

        File file = FileUtils.multipartFileToFile(multipartFile);
        try {
            HttpResponse response = JdcloudSDKClient.upload(jdCloudConfig.accessKey, jdCloudConfig.secretKey, Protocol.HTTPS, jdCloudConfig.endPoint,
                    jdCloudConfig.ocrBusinessPath, jdCloudConfig.method, headers, queryMap, file);
            String result = new String(BinaryUtils.toByteArray(response.getContent()));
            System.out.println(result);
            JdCloudResultDTO<BusinessResultDTO> jdCloudResultDTO = new Gson().fromJson(result, JdCloudResultDTO.class);
            System.out.println(jdCloudResultDTO.getResult().getResultData());
            FileUtils.delTempFile(file);
            return jdCloudResultDTO.getResult().getResultData();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 营业执照识别
     *
     * @param multipartFile
     * @author: wqh
     * @param: multipartFile
     * @return: Object
     */
    @Override
    public Object getInvoiceInfo(MultipartFile multipartFile) throws Exception {
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();

        File file = FileUtils.multipartFileToFile(multipartFile);
        try {
            HttpResponse response = JdcloudSDKClient.upload(jdCloudConfig.accessKey, jdCloudConfig.secretKey, Protocol.HTTPS, jdCloudConfig.endPoint,
                    jdCloudConfig.ocrInvoicePath, jdCloudConfig.method, headers, queryMap, file);
            String result = new String(BinaryUtils.toByteArray(response.getContent()));
            System.out.println(result);
            JdCloudResultDTO<InvoiceResultDTO> jdCloudResultDTO = new Gson().fromJson(result, JdCloudResultDTO.class);
            System.out.println(jdCloudResultDTO.getResult().getResult());
            FileUtils.delTempFile(file);
            return jdCloudResultDTO.getResult().getResult();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
}
